import "dotenv/config";
import Koa from "koa";
import KoaBody from "koa-body";

import logger from "./middlewares/logger";
import checkError from "./middlewares/checkError";
import loginFilter from "./middlewares/loginFilter";
import router from './routers'

import * as db from "./db";

const app = new Koa({
  keys: JSON.parse(process.env.KEYS),
});
// 添加日志和执行异常检查的中间件
app.use(logger);
app.use(checkError);
// 添加body解析中间件
app.use(KoaBody());
app.use(loginFilter);    // 登陆拦截器中间件
// 添加路由
app.use(router.routes());

async function run() {
  // 先等待数据库连接
  await db.init();
  // 监听端口
  app.listen(process.env.PORT);
}

run();
